package net.oschina.git.fsppp.view.frame;

import net.oschina.git.fsppp.common.base.BaseEvent;
import net.oschina.git.fsppp.common.base.BaseFrame;
import net.oschina.git.fsppp.model.P3Dir;
import net.oschina.git.fsppp.utils.P3Utils;

import javax.swing.*;
import java.awt.*;

/**
 * Created by qihuang on 16-10-21.
 */
public class EditView extends BaseFrame {
    private P3Dir dir, dirClone;
    private TextArea textArea;
    private JButton btnQuit, btnSave, btnSaveQ;
    private boolean isEditByOther;

    public void setEditByOther(boolean editByOther) {
        isEditByOther = editByOther;
    }


    public EditView(BaseFrame pre) {
        super();
        preFrame = pre;
    }


    @Override
    protected void initView() {
        dir = (P3Dir) BaseEvent.getDefault().getParam();
        try {
            dirClone = (P3Dir) dir.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        btnQuit = new JButton("退出");
        btnSave = new JButton("保存");
        btnSaveQ = new JButton("保存并退出");
        btnSave.setBounds(10, 10, 80, 25);
        btnSaveQ.setBounds(100, 10, 150, 25);
        btnQuit.setBounds(260, 10, 80, 25);
        textArea = new TextArea();
        Byte[] bytes = dir.getContent();
        if (bytes != null)
            textArea.setText(new String(P3Utils.Byte2byte(bytes)));
        textArea.setBounds(5, 40, 580, 630);
        setLayout(null);
        add(textArea);
        add(btnSave);
        add(btnSaveQ);
        add(btnQuit);
        setSize(600, 700);
        if (dir.isOpen()) {
            isEditByOther = true;
            textArea.setEditable(false);
            btnSaveQ.setEnabled(false);
            btnSave.setEnabled(false);
            showMsg("文件已被锁定，当前为只读模式");
        } else {
            dir.setIsOpen(true);
        }
    }

    @Override
    protected void initPM() {

    }

    @Override
    protected void initListener() {
        btnQuit.addActionListener(e -> {
            resultCode = 0;
            dispose();
        });
        btnSave.addActionListener(e -> {
            saveContent(false);
        });
        btnSaveQ.addActionListener(e -> {
            saveContent(true);
        });

    }

    @Override
    protected String getFrameTitle() {
        return dir.getName();
    }

    @Override
    public void dispose() {
        super.dispose();
        close();
    }

    private void close() {
        if (!isEditByOther) {
            BaseEvent.getDefault().setParam(dirClone);
            preFrame.onFrameResult(resultCode);
        }
        dir.setIsOpen(false);
    }

    private void saveContent(boolean quit) {
        byte[] bytes = textArea.getText().getBytes();
        Byte[] bs = new Byte[bytes.length];
        for (int i = 0; i < bytes.length; i++) {
            bs[i] = new Byte(bytes[i]);
        }
        dirClone.setContent(bs);
        resultCode = 1;
        if (quit) {
            dispose();
        }
    }

    @Override
    protected void childDoClose() {
        close();
    }
}
